﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses
Imports System.IO
Imports System.Security.Cryptography

Public Class Register
    Inherits System.Web.UI.Page

    Private rand As New System.Random

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                Me.Session("CaptchaImageText") = GenerateRandomCode()
                'btnRegister.Attributes.Add("onclick", "checkform();")
                If LCase(Request.QueryString("role")) = "b2b" Then
                    plCompany.Visible = True
                    Session("Breadcrumb") = "&nbsp;&raquo;&nbsp;Reseller Sign Up"
                    rdType.SelectedValue = "b2b"
                Else
                    plCompany.Visible = False
                    Session("Breadcrumb") = "&nbsp;&raquo;&nbsp;Client Sign Up"
                    rdType.SelectedValue = "b2c"
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub txtEmail_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
        Dim txt As Array
        txt = RetriveData("select Id from ptn_Users where Email='" & txtEmail.Text & "'", 0)
        If txt(0) Is Nothing Then
            EmailAvailability.InnerText = "Available"
            EmailAvailability.Attributes.Add("class", "available")
        Else
            EmailAvailability.InnerText = "Not Available"
            EmailAvailability.Attributes.Add("class", "not-available")
        End If
        'Dim EncryptedText As String = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5")
        'Dim EncryptedText As String = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5")
        'Dim DcryptedText As String = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5")
        'Dim vPass As Boolean = VerifyHash(txt(1), "SHA512", lblMsg.Text)
    End Sub

    Private Function GenerateRandomCode() As String
        Dim s As String = ""
        For i As Integer = 0 To 5
            s = s + rand.Next(10).ToString()
        Next
        Return s
    End Function

    Private Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Try
            Dim tempString As String = Me.Session("CaptchaImageText")
            Dim strSql As String
            If chkTerms.Checked = True Then

                'If String.Compare(tempString, txtCaptcha.Text.Trim()) = 0 Then
                Dim strNewsletter As String
                Dim strBody As New StringBuilder
                Dim verifyId As String
                If chkNewsletter.Checked = True Then
                    strNewsletter = 1
                Else
                    strNewsletter = 0
                End If

                Randomize()
                Dim temp1, temp2 As String
                temp1 = Replace(TimeOfDay, " ", "")
                temp1 = Replace(temp1, ":", "")
                temp2 = Replace(Date.Today, " ", "")
                temp2 = Replace(temp2, "/", "")
                temp2 = Replace(temp2, ".", "")
                verifyId = temp2 & temp1 & ((9999999999 - 5 + 1) * Rnd() + 5)
                verifyId = Replace(verifyId, ".", "")
                verifyId = Replace(verifyId, "+", "")
                verifyId = Replace(verifyId, "E", "")

                Dim strUsername As String() = txtEmail.Text.Split("@"c)

                'Dim EPass As String = ComputeHash(txtPassword.Text, "SHA512", Nothing)
                Dim strPass As String = Encryptdata(txtPassword.Text)
                MsgBox(strPass)
                Dim arr As Array
                arr = RetriveData("Select Id from ptn_Users where Email='" & Trim(txtEmail.Text) & "'", 0)

                If arr(0) Is Nothing Then
                    'If LCase(Request.QueryString("role")) Is Nothing Or LCase(Request.QueryString("role")) = "b2c" Then
                    '    strSql = "INSERT INTO [ptn_Users] ([ClientType], [Username], [Email], [Password],"
                    '    strSql += "[FirstName], [LastName], [Address1], [Address2], [City], [Country],"
                    '    strSql += "[Phone], [Mobile], [AltEmail], [Learn], [Comments],"
                    '    strSql += "[Newsletter], [IsAdmin], [ViewOrder], [IsActive], [IsB2B], [VerifyId], [CreatedOn], [UpdatedOn]) "
                    '    strSql += "VALUES('B2C','" & strUsername(0) & "','" & txtEmail.Text.Replace("'", "&#039;") & "','" & strPass & "'"
                    '    strSql += ",'" & txtFName.Text.Replace("'", "&#039;") & "','" & txtLName.Text.Replace("'", "&#039;") & "','" & txtAdd1.Text.Replace("'", "&#039;") & "'"
                    '    strSql += ",'" & txtAdd2.Text.Replace("'", "&#039;") & "','" & txtCity.Text.Replace("'", "&#039;") & "','" & cmbCountry.SelectedValue & "'"
                    '    strSql += ",'" & txtPhone.Text.Replace("'", "") & "','" & txtMobile.Text.Replace("'", "") & "','" & txtAltEmail.Text.Replace("'", "") & "','" & cmbLearn.SelectedValue & "','" & txtComments.Text.Replace("'", "") & "'"
                    '    strSql += ",'" & strNewsletter & "','0','0','0','0','" & verifyId & "','" & Date.Now & "','" & Date.Now & "')"
                    '    Insert_Update_Delete_Record(strSql)
                    'Else
                    '    strSql = "INSERT INTO [ptn_Users] ([ClientType], [Username], [Email], [Password],"
                    '    strSql += "[FirstName], [LastName], [Address1], [Address2], [City], [Country],"
                    '    strSql += "[Phone], [Mobile], [AltEmail], [Learn], [Comments],"
                    '    strSql += "[CompanyName], [CAddress], [CContact], [CWebsite], [CEmail],"
                    '    strSql += "[Newsletter], [IsAdmin], [ViewOrder], [IsActive], [IsB2B], [VerifyId], [CreatedOn], [UpdatedOn]) "
                    '    strSql += "VALUES('B2B','" & strUsername(0) & "','" & txtEmail.Text.Replace("'", "&#039;") & "','" & strPass & "'"
                    '    strSql += ",'" & txtFName.Text.Replace("'", "&#039;") & "','" & txtLName.Text.Replace("'", "&#039;") & "','" & txtAdd1.Text.Replace("'", "&#039;") & "'"
                    '    strSql += ",'" & txtAdd2.Text.Replace("'", "&#039;") & "','" & txtCity.Text.Replace("'", "&#039;") & "','" & cmbCountry.SelectedValue & "'"
                    '    strSql += ",'" & txtPhone.Text.Replace("'", "") & "','" & txtMobile.Text.Replace("'", "") & "','" & txtAltEmail.Text.Replace("'", "") & "','" & cmbLearn.SelectedValue & "','" & txtComments.Text.Replace("'", "") & "'"
                    '    strSql += ",'" & txtCName.Text.Replace("'", "") & "','" & txtCAddress.Text.Replace("'", "") & "','" & txtCAddress.Text.Replace("'", "") & "','" & txtContact.Text.Replace("'", "") & "','" & txtWebsite.Text.Replace("'", "") & "','" & txtCEmail.Text.Replace("'", "") & "'"
                    '    strSql += ",'" & strNewsletter & "','0','0','0','1','" & verifyId & "','" & Date.Now & "','" & Date.Now & "')"
                    '    Insert_Update_Delete_Record(strSql)
                    'End If

                    
                    strSql = "INSERT INTO [ptn_Users] ([ClientType], [Username], [Email], [Password],"
                    strSql += "[FirstName], [LastName], [Address1], [Address2], [City], [Country],"
                    strSql += "[Phone], [Mobile], [AltEmail], [Learn], [Comments],"
                    'If Not LCase(Request.QueryString("role")) Is Nothing Or LCase(Request.QueryString("role")) = "b2b" Then
                    strSql += "[CompanyName], [CAddress], [CContact], [CWebsite], [CEmail], [IsB2B],"
                    'End If
                    strSql += "[Newsletter], [IsAdmin], [ViewOrder], [IsActive], [VerifyId], [CreatedOn], [UpdatedOn]) "
                    strSql += "VALUES('" & rdType.SelectedValue & "','" & strUsername(0) & "','" & txtEmail.Text.Replace("'", "&#039;") & "','" & strPass & "'"
                    strSql += ",'" & txtFName.Text.Replace("'", "&#039;") & "','" & txtLName.Text.Replace("'", "&#039;") & "','" & txtAdd1.Text.Replace("'", "&#039;") & "'"
                    strSql += ",'" & txtAdd2.Text.Replace("'", "&#039;") & "','" & txtCity.Text.Replace("'", "&#039;") & "','" & cmbCountry.SelectedValue & "'"
                    strSql += ",'" & txtPhone.Text.Replace("'", "") & "','" & txtMobile.Text.Replace("'", "") & "','" & txtAltEmail.Text.Replace("'", "") & "','" & cmbLearn.SelectedValue & "','" & txtComments.Text.Replace("'", "") & "', "
                    If Not LCase(Request.QueryString("role")) Is Nothing Or LCase(Request.QueryString("role")) = "b2b" Then
                        strSql += "'" & txtCName.Text.Replace("'", "") & "','" & txtCAddress.Text.Replace("'", "") & "','" & txtContact.Text.Replace("'", "") & "','" & txtWebsite.Text.Replace("'", "") & "','" & txtCEmail.Text.Replace("'", "") & "','1', "
                    Else
                        strSql += "'','','','','','0', "
                    End If
                    strSql += "'" & strNewsletter & "','0','0','0','" & verifyId & "','" & Date.Now & "','" & Date.Now & "')"
                    'Insert_Update_Delete_Record(strSql)

                    strBody.Append("<b>DETAILS GIVEN AT THE TIME OF REGISTRATION ON THE WEBSITE</b>")

                    If LCase(rdType.SelectedValue) = "b2b" Then
                        strBody.Append("<p style=""font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333"">Dear " & txtFName.Text & ",<br> <br>")
                        strBody.Append("Thank you for Choosing <a href=""http://www.philatourism.com"">www.philatourism.com</a> as a Business Partner, we have received your registration request as per the details below:<br><br>")
                    Else
                        strBody.Append("<p style=""font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333"">Dear " & txtFName.Text & ",<br> <br>")
                        strBody.Append("Thank you for Choosing <a href=""http://www.philatourism.com"">www.philatourism.com</a>, we have received your registration request as per the details below:<br><br>")
                    End If

                    strBody.Append("<table cellspacing=""0"" cellpadding=""6"" border=""0"" align=""center"" width=""99%"">")

                    'If Not Request.QueryString("vid") Is Nothing Then
                    strBody.Append("<tr><td colspan=""2""><p>Please <a href=""http://philatourism.com/philanew/Verify.aspx?vid=" & verifyId & """>click here</a> to verfy your account.</p></td></tr>")
                    'End If

                    If LCase(rdType.SelectedValue) = "b2b" Then
                        strBody.Append("<tr><td colspan=""2"">Company Details</td></tr>")
                        strBody.Append("<tr><td>Company Name: </td><td valign=""top"">" & txtCName.Text & "</td></tr>")
                        strBody.Append("<tr><td>Company Address: </td><td valign=""top"">" & txtCAddress.Text & "</td></tr>")
                        strBody.Append("<tr><td>Contact Details: </td><td valign=""top"">" & txtContact.Text & "</td></tr>")
                        '    strBody.Append("<tr><td>Website: </td><td valign=""top"">" & txtWebsite.Text & "</td></tr>")
                        strBody.Append("<tr><td>Email: </td><td valign=""top"">" & txtCompEmail.Text & "</td></tr>")
                    End If

                    strBody.Append("<tr><td colspan=""2"">Personal Details</td></tr>")
                    strBody.Append("<tr><td width=""180"" valign=""top"">Your Email Address:</td><td valign=""top"">" & txtEmail.Text & "</td></tr>")
                    strBody.Append("<tr><td>First Name: </td><td>" & txtFName.Text & "</td></tr><tr><td>Last Name: </td><td>" & txtLName.Text & "</td></tr>")
                    strBody.Append("<tr><td>Address 1: </td><td>" & txtAdd1.Text & "</td></tr><tr><td>Address 2: </td><td>" & txtAdd2.Text & "</td></tr>")
                    strBody.Append("<tr><td>City: </td><td>" & txtCity.Text & "</td></tr><tr><td>Country: </td><td>" & cmbCountry.SelectedItem.Text & "</td></tr>")
                    strBody.Append("<tr><td>Phone Number: </td><td>" & txtPhone.Text & "</td></tr><tr><td>Mobile Number: </td><td>" & txtMobile.Text & "</td></tr>")
                    strBody.Append("<tr><td>Alternate Email: </td><td>" & txtAltEmail.Text & "</td></tr>")
                    strBody.Append("<tr><td>How did you learn about Phila? </td><td>" & cmbLearn.SelectedItem.Text & "</td></tr>")
                    strBody.Append("<tr><td>Comments </td><td class=""smalltext"">" & txtComments.Text & "</td></tr>")
                    strBody.Append("</table>")

                    If LCase(rdType.SelectedValue) = "b2b" Then
                        strBody.Append("Please wait for the Confirmation email to follow in few minutes. Thank you for the patience and thank you once again for choosing <a href=""http://www.philatourism.com"">www.philatourism.com</a> as a Business Partner.")
                    Else
                        strBody.Append("Please wait for the Confirmation email to follow in few minutes. Thank you for the patience and thank you once again for choosing <a href=""http://www.philatourism.com"">www.philatourism.com</a>.")
                    End If

                    strBody.Append("<br /><br />With best regards,<br> Phila Tours & Travels Pvt. Ltd.<br>")
                    strBody.Append("<a href=""http://www.philatourism.com"" target=""_new"" style=""color:#db261f"">http://www.philatourism.com</a><br>")

                    If Sendmail("info@philatourism.com", txtEmail.Text, "Philatourism.com account has been created", strBody.ToString, "", False) = True Then
                        Response.Redirect("Verify.aspx?role=" & rdType.SelectedValue)
                    Else
                        lblMsg.Text = "<h4 align=""center"" class=""red"">Error has occured while creating account.</h4>"
                        lblMsg.ForeColor = Drawing.Color.Red
                    End If
                Else
                    lblMsg.Text = "Emailid already exists."
                    lblMsg.ForeColor = Drawing.Color.Red
                End If
                'Else
                '    lblCaptcha.Text = "<br />The typed code must match the image, please try again"
                '    txtCaptcha.Text = ""
                '    Me.Session("CaptchaImageText") = GenerateRandomCode()
                'End If
            Else
                lblMsg.Text = "Please accept Terms & Conditions."
                lblMsg.ForeColor = Drawing.Color.Red
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class